﻿using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Input;
using Acr.UserDialogs;
using Cashbook.Models;
using Cashbook.Services;
using Cashbook.Views;
using Xamarin.Forms;

namespace Cashbook.ViewModels
{
    public class BillsListVM: AbstractViewModel
    {
		public ObservableRangeCollection<BillVM> Bills { get; set; }

        private BillsDataAccess _billsService;
		private INavigation _navigation; 

        public Command LoadBillsCommand { get; set; }
		protected IUserDialogs _dalogs { get; }
        	

		
		bool _isPayed;
		public bool IsPayed
		{
			get { return this._isPayed; }
			set
			{
				if (this._isPayed == value)
					return;

				this._isPayed = value;
				this.OnPropertyChanged();
			}
		}
		
        bool _isBusy;
        public bool IsBusy
		{
			get { return this._isBusy; }
			set
			{
				if (this._isBusy == value)
					return;

				this._isBusy = value;
				this.OnPropertyChanged();
			}
		}

		string _title;
		public string Title
		{
			get { return this._title; }
			set
			{
				if (this._title == value)
					return;

				this._title = value;
				this.OnPropertyChanged();
			}
		}


		public BillsListVM(bool isPayed, INavigation navigation, IUserDialogs dialogs) : base(dialogs)
        {
			_navigation = navigation;

			IsPayed = isPayed;
            Title = IsPayed ? "已结帐单" : "未结帐单";
          

			_billsService = new BillsDataAccess();
            this.Bills = new ObservableRangeCollection<BillVM>(); 

            LoadBillsCommand = new Command(async () =>  await ExecuteLoadBillsCommand());

            AddBillCommand = new Command(() =>  _navigation.PushAsync(new EditBillPage(0)));
            EditCommand = new Command<BillVM>(async (model) => await _navigation.PushAsync(new EditBillPage(model.Id)));
            StatementCommand = new Command<BillVM>(async (model) => await _navigation.PushAsync(new EditStatementPage(model)));
            DeleteCommand = new Command<BillVM>(async (model) => await ExecuteDeleteCommand(model));
        }

        async Task ExecuteDeleteCommand(BillVM model)
        {
			var result = await this.Dialogs.ConfirmAsync("您确定要删除当前所选帐单吗，此操作不可恢复？", "删除帐单", "确定","取消");
		
			  if (result)
			  {
	                var bill = await _billsService.GetBillAsync(model.Id);
				_billsService.DeleteBillWithStatementAsync(bill);

				Bills.Remove(model);
			  }
		
		}

      

        async Task ExecuteLoadBillsCommand()
        {
            if (IsBusy)
                return;

            IsBusy = true;

            try
            {
                Bills.Clear();
                var items = _billsService.GetBillsWithCustomer(IsPayed);
                Bills.ReplaceRange(items);
            }
            catch (Exception ex)
            {
               // Debug.WriteLine(ex);
                await this.Dialogs.AlertAsync("帐单加载失败." + ex.Message, "错误", "确定");               
            }
            finally
            {
                IsBusy = false;
            }
        }

        /// <summary>
        /// Command to open browser to xamarin.com
        /// </summary>
        public ICommand AddBillCommand { get; }
        public ICommand EditCommand { get; private set; }
        public ICommand StatementCommand{ get; private set; }
        public ICommand DeleteCommand { get; private set; }
		
    }
}
